/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package driver;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import xml.XMLHelper;


/**
 *
 * @author keith
 */
public class Bouml {

  final private File boumlProj;
  final private File plugoutJar;
  final private File outDir;
  final private File xslFile;


  public Bouml( File boumlProj, File plugoutJar, File xslFile, File outDir ) {
    this.boumlProj = boumlProj;
    this.plugoutJar = plugoutJar;
    this.outDir = outDir;
    this.xslFile = xslFile;
  }


  private Document extractUmlDoc()
    throws
    IOException {
    Document umlDoc = null;
    final List<String> cmdList = new ArrayList<String>();

    cmdList.add( "bouml" );
    cmdList.add( boumlProj.getCanonicalPath() );
    cmdList.add( "-exec" );
    cmdList.add( "java" );
    cmdList.add( "-jar" );
    cmdList.add( plugoutJar.getCanonicalPath() );
    cmdList.add( "-exit" );

    ProcessBuilder pb = new ProcessBuilder( cmdList );

    try {
      pb.directory( outDir );

      final File imgDir = new File( "bouml" );
      final File umlDocFile = new File( outDir, "umldoc.xml" );

      final File genRelImg = new File( outDir, imgDir.getPath() );

      boolean haveImgDir = genRelImg.exists();
      if ( !haveImgDir ) {
        haveImgDir = genRelImg.mkdirs();
      }
      if ( !haveImgDir ) {
        System.out.println(
          "can't ensure image directory:" + genRelImg.getAbsolutePath() );
      } else {
        Map<String, String> env = pb.environment();
        env.put( "bouml_out", umlDocFile.getCanonicalPath() );
        env.put( "bouml_img_dir", imgDir.getPath() );

        Process t = pb.start();

        int exitVal = t.waitFor();

        System.out.println( "exitVal = " + exitVal );

        if ( umlDocFile.exists() ) {
          umlDoc = XMLHelper.parse( umlDocFile );
        }
      }
    } catch ( Exception ex ) {
      ex.printStackTrace();
    }

    return umlDoc;
  }


  public void driver() throws IOException,
                              FileNotFoundException,
                              SAXException,
                              TransformerConfigurationException,
                              ParserConfigurationException,
                              TransformerException {
    Document doc = extractUmlDoc();

//    XMLHelper.dump( doc );

    Document xslDoc = XMLHelper.parse( xslFile );
//    XMLHelper.dump( xslDoc );
    Transformer transformer = XMLHelper.getTransformer( xslDoc );

    DOMResult result = new DOMResult();
    Source docSrc = new DOMSource( doc );

    transformer.setParameter( "outdir", outDir.getCanonicalFile() );
    transformer.transform( docSrc, result );

//    XMLHelper.dump( result.getNode() );
  }


  public static void main( String[] args ) throws IOException,
                                                  FileNotFoundException,
                                                  SAXException,
                                                  TransformerConfigurationException,
                                                  ParserConfigurationException,
                                                  TransformerException {
    final File generated = new File( "generated" );
    final File boumlProj = new File(
      "../../design-doc/Semester Project/IPFW_MUD_Server/IPFW_MUD_Server.prj" );
    final File plugoutJar = new File( "../UML2DocBook/dist/UML2DocBook.jar" );

    File xslFile = new File( "config/section_builder.xsl" );

    if ( xslFile.exists() ) {
      Bouml b = new Bouml( boumlProj, plugoutJar, xslFile, generated );

      b.driver();
    }

  }
}
